import Vue from 'vue'
import VueRouter from 'vue-router'
import Layout from '../components/Layout.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [
      {
        path: 'dashboard',
        name: 'Dashboard',
        component: () => import('../views/dashboard/index.vue'),
        meta: { title: '工作台' }
      },
      {
        path: 'order',
        name: 'Order',
        component: () => import('../views/order/index.vue'),
        meta: { title: '订单管理' }
      },
      {
        path: 'category',
        name: 'Category',
        component: () => import('../views/category/index.vue'),
        meta: { title: '分类管理' }
      },
      {
        path: 'dish',
        component: { render: h => h('router-view') },
        meta: { title: '菜品管理' },
        children: [
          {
            path: '',
            name: 'Dish',
            component: () => import('../views/dish/index.vue'),
            meta: { title: '菜品管理' }
          },
          {
            path: 'add',
            name: 'DishAdd',
            component: () => import('../views/dish/add-edit.vue'),
            meta: { title: '新增菜品', activeMenu: '/dish' }
          },
          {
            path: 'edit/:id',
            name: 'DishEdit',
            component: () => import('../views/dish/add-edit.vue'),
            meta: { title: '编辑菜品', activeMenu: '/dish' }
          }
        ]
      },
      {
        path: 'setmeal',
        component: { render: h => h('router-view') },
        meta: { title: '套餐管理' },
        children: [
          {
            path: '',
            name: 'Setmeal',
            component: () => import('../views/setmeal/index.vue'),
            meta: { title: '套餐管理' }
          },
          {
            path: 'add',
            name: 'SetmealAdd',
            component: () => import('../views/setmeal/add-edit.vue'),
            meta: { title: '新增套餐', activeMenu: '/setmeal' }
          },
          {
            path: 'edit/:id',
            name: 'SetmealEdit',
            component: () => import('../views/setmeal/add-edit.vue'),
            meta: { title: '编辑套餐', activeMenu: '/setmeal' }
          }
        ]
      },
      {
        path: 'statistics',
        name: 'Statistics',
        component: () => import('../views/statistics/index.vue'),
        meta: { title: '数据统计' }
      },
      {
        path: 'employee',
        name: 'Employee',
        component: () => import('../views/employee/index.vue'),
        meta: { title: '员工管理' }
      },
      {
        path: 'shop',
        name: 'Shop',
        component: () => import('../views/shop/index.vue'),
        meta: { title: '店铺管理' }
      }
    ]
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/login/index.vue'),
    meta: { title: '登录' }
  },
  // 404页面
  {
    path: '*',
    redirect: '/'
  }
]

const router = new VueRouter({
  routes
})

// 全局前置守卫
router.beforeEach((to, from, next) => {
  // 获取token
  const token = localStorage.getItem('token')
  
  // 如果是登录页面，直接放行
  if (to.path === '/login') {
    next()
    return
  }
  
  // 如果没有token，跳转到登录页面
  if (!token) {
    next('/login')
    return
  }
  
  // 有token，放行
  next()
})

export default router
